#include "capturer_track_source.hpp"
#include "p2pms_log.hpp"

#include "modules/video_capture/video_capture_factory.h"

namespace p2pms 
{

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
rtc::scoped_refptr<CapturerTrackSource> CapturerTrackSource::Create(size_t width, size_t height,
	size_t fps, const std::string& device_id)
{
	if (device_id.empty()) {
		LOG_ERR("Device ID is empty for video capturer");
		return nullptr;
	}

	LOG_INF("Creating video capturer for device: {}, resolution: {}x{}, fps: {}", 
		device_id, width, height, fps);

	auto capturer = absl::WrapUnique(VideoCapturer::Create(width, height, fps, device_id));
	if (capturer) {
		LOG_INF("Video capturer created successfully");
		return rtc::make_ref_counted<CapturerTrackSource>(std::move(capturer));
	}

	LOG_ERR("Failed to create video capturer for device: {}", device_id);
	return nullptr;
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
CapturerTrackSource::CapturerTrackSource(std::unique_ptr<VideoCapturer> capturer)
	: VideoTrackSource(/*remote=*/false), m_capturer(std::move(capturer)) 
{
	
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
rtc::VideoSourceInterface<webrtc::VideoFrame>* CapturerTrackSource::source() 
{
	return m_capturer.get();
}

} // namespace p2pms